package utils
{
	
	//import com.greensock.TimelineLite;
	//import com.greensock.TweenLite;
	//import com.greensock.TweenMax;
	
	//import com.greensock.TimelineLite;
	import com.greensock.TimelineMax;
	import com.greensock.TweenMax;
	import com.greensock.easing.Elastic;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.InteractiveObject;
	import flash.display.Sprite;
	import flash.filters.BitmapFilter;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.ColorMatrixFilter;
	import flash.filters.GlowFilter;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	/**
	 *  显示对象的相关处理。
	 * @author 青竹
	 * 
	 */
	public class DisplayObjectUtil
	{	
		/**
		 * 黑白颜色
		 * @param d
		 *
		 */
		public static var colors:Array=[new ColorMatrixFilter([0.3, 0.59, 0.11, 0, 0, 0.3, 0.59, 0.11, 0, 0, 0.3, 0.59, 0.11, 0, 0, 0, 0, 0, 1, 0])];

		/**
		 * 设置一个对象失效并为黑白 
		 * @param d
		 * @param v
		 * 
		 */
		public static function setEnabled(d:InteractiveObject,v:Boolean=false):void
		{
			if(v){
			   d.filters=null;
			}else{
			   setGrayscale(d);  
			}
			d.mouseEnabled = v;
			if(d is DisplayObjectContainer)
			{
				(d as DisplayObjectContainer).mouseChildren = v;
			}		
		}
		
				/**
		 *  灰度
		 * @param arg
		 * 
		 */
		public static function setGrayscale(...arg):void{
			for(var i:int=0;i<arg.length;i++){
			   arg[i].filters=colors;
			}
		}
		
		/**
		 * 将显示对象某个区域设置成黑白模式。 
		 * @param display 显示对象
		 * @param rect 区域。
		 *  @形状
		 */
		public static function setDisplayObjectGrayscale(display:DisplayObjectContainer,rect:Rectangle,isCir:Boolean=true):DisplayObject{
			var w:Number=rect.width;
			var h:Number=rect.height;
			var bitmapData:BitmapData = new BitmapData(w,h,false,0xff0000);
			var m:Matrix=new Matrix();
			m.tx=-rect.x;
			m.ty=-rect.y;
			bitmapData.draw(display,m);
			
			var c:Sprite=new Sprite;
			c.addChild(new Bitmap(bitmapData));
			
			if(isCir){
				var mk:Sprite=new Sprite();
				mk.graphics.beginFill(0xff0000);
				mk.graphics.drawCircle(0,0,10);
				mk.graphics.endFill();
				mk.x=w/2;
				mk.y=h/2;
				mk.width=w;
				mk.height=h
				c.mask=mk;
				c.addChild(mk);
			}
			DisplayObjectUtil.setGrayscale(c);
			c.x=rect.x;
			c.y=rect.y;
			display.addChild(c);
			return c;
		}
		
		/**
		 * 对bitmapData进行缩放，抗锯齿,消耗内存。
		 * 一般对于抗锯齿，可以设置Bitmap.smoothing=true即可，对于抗锯要求很高的，可以采用此方法。
		 * @param bitmapData
		 * @return 
		 * 
		 */
		public static function scaleBitmapData(bitmapData:BitmapData,width:Number,height:Number):BitmapData{
			var a:BitmapData=bitmapData;
			var mxp:Matrix=new Matrix()
			mxp.scale(width/a.width,height/a.height);
			var sp:BitmapData=new BitmapData(width,height,true,0);
			sp.draw(a,mxp,null,null,null,true);
			return sp;
		}
		
		/**
		 * 获取相对舞台坐标。 
		 * @param dis
		 * @return 
		 * 
		 */
		public static function getStagePostion(dis:DisplayObject):Point{
			if(dis.stage==null) return null;
			var temp:DisplayObject=dis;
			var p:Point = new Point();
			while(temp){
				p.x+=temp.x;
				p.y+=temp.y;
				temp=temp.parent;	
			}
			return p;
		}
		
		import flash.geom.ColorTransform;
		public static function setMcColor(mc:DisplayObject,color:uint):void{
			var cdd:ColorTransform=new ColorTransform;//mc.transform.colorTransform
			cdd.color=color
			cdd.blueMultiplier=1
			cdd.redMultiplier=1
			cdd.greenMultiplier=1;
			mc.transform.colorTransform=cdd;
		}
		//public static function bg
		public static function getBitmapFilter(color:uint=0x009900,blurX:Number=10,blurY:Number=10,strength:Number=5,alpha:Number=1,quality:Number = BitmapFilterQuality.HIGH):BitmapFilter {
			//var color:Number = 0x0099FF;
			//var alpha:Number = 1;
			//var blurX:Number = 10;
			//var blurY:Number = 10;
			//var strength:Number = 5;
			var inner:Boolean = false;
			var knockout:Boolean = false;
			var quality:Number = quality;
			return new GlowFilter(color,
				alpha,
				blurX,
				blurY,
				strength,
				quality,
				inner,
				knockout);
		}
		
		/**
		 * 使一个显示对象不停颤抖
		 * @param displayObject
		 * @param amount 颤抖次数。
		 * 
		 */
		public static function setDisplayTremble(displayObject:*,amount:int=15):TimelineMax{
		   	var list:TimelineMax = new TimelineMax({repeat:amount, yoyo:false, repeatDelay:0.5});
			list.append(TweenMax.to(displayObject,.5,{scaleX:.8,scaleY:.8}));
			list.append(TweenMax.to(displayObject,.6,{scaleX:1,scaleY:1,ease:Elastic.easeOut}));
			return list;
		}
		
		/**
		 * 将一个显示容器内的元件，相对 cotainnerWidth宽度居中。
		 * @param contain
		 * @param cotainnerWidth
		 * 
		 */
		public static function setDisplaySort(contain:DisplayObjectContainer,cotainnerWidth:Number):void{
			  var s:DisplayObject;
			  var o:Number=0;
		      for(var i:int=0;i<contain.numChildren;i++){
				  if(s){
				  	 o=s.x+s.width;
				  }
			      s = contain.getChildAt(i);
				  s.x=o;

			  }
			 if(s==null) return;
			  var off:Number=(cotainnerWidth-(s.x+s.width))/2;
			  for(i=0;i<contain.numChildren;i++){
				  s = contain.getChildAt(i);
				  s.x+=off;
			  }
		}
		//坐标
		/**
		 * 指定角度，计算旋转坐标后坐标。
		 * @param locationX 
		 * @param locationY
		 * @param rotation 旋转角度(0-360)
		 * @return 旋转后的坐标
		 * 
		 */
		public function ratationCoord(locationX:int,locationY:int,rotation:Number):Point{
			var f:Number=Math.PI/180;
			var s:Number = Math.sin((rotation)*f);
			var c:Number = Math.cos((rotation)*f);
			var px:Number = locationX*c - locationY*s;
			var py:Number = locationY*c + locationX*s;
			return new Point(px,py);
		}
	}
}